@extends('admin.layout')
@section('title', $title)
@section('content')
	<div class="content-wrapper" style="min-height: 946px;">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                商品管理
                <small>商品添加</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li><a href="#">商品管理</a></li>
                <li class="active">商品添加</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <!-- left column -->
                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h6 class="btn btn-primary">商品信息</h6>
                            <h6 class="btn">商品描述</h6>
                            <h6 class="btn">商品类型</h6>
                            <h6 class="btn">商品相册</h6>
                        </div><!-- /.box-header -->
                        <!-- form start -->
                     @if (count($errors) > 0)
                      <div id="jg" class="callout callout-info">
                        <h4>警告</h4>
                        @foreach ($errors->all() as $error)
                        <p>{{ $error }}</p>
                        @endforeach
                      </div>
                       @endif
                        @if(session('info'))
                        <div id="jg" class="callout callout-info">
                        <h4>{{ session('info') }}</h4> 
                        </div>   
                        @endif 
                        <form role="form" action="{{ url('/admin/goods/insert') }}" method="post" enctype="multipart/form-data">
                        	{{ csrf_field()}}
                            <div class="box-body">
		                         <div class="form-group">
		                            <label for="exampleInputEmail1">主分类</label>
		                            <select class="form-control" name="cat_id">
                                    @foreach($category as $v)
		                            	<option value="{{ $v -> id }}">{{ $v -> name }}</option>
                                    @endforeach
		                            </select>
		                         </div>
                                 <div class="form-group" id="kuozhan">
                                    <label for="exampleInputEmail1">扩展分类<a onclick="addKuozhan(this)" href="#">[+]</a></label>
                                    <select class="form-control" name="ext_cat_id[]">
                                    <option value="">请选择</option>
                                    @foreach($category as $v)
                                        <option value="{{ $v -> id }}">{{ $v -> name }}</option>
                                    @endforeach
                                    </select>
                                 </div>                                                              
                                <div class="form-group" id="goods_name">
                                    <label for="exampleInputEmail1">商品名称</label>
                                    <input type="text" name="goods_name" class="form-control" id="exampleInputEmail1" placeholder="商品名称">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputPassword1">商品价格</label>
                                    <input type="text"  name="goods_price" class="form-control" id="exampleInputPassword1" placeholder="商品价格">
                                </div>
                                <div class="form-group">
                                	<div class="row">
                                		<div class="col-md-4">
		                                    <label for="exampleInputPassword1">促销价格</label>
		                                    <input type="text" name="promote_price" class="form-control" id="exampleInputPassword1" placeholder="促销价格">
	                                    </div>
                                		<div class="col-md-4">
		                                    <label for="exampleInputPassword1">促销开始时间</label>
		                                    <input  onClick="WdatePicker()" type="text" name="promote_start_time"  class="form-control" id="exampleInputPassword1" placeholder="促销开始时间">
	                                    </div>
                                		<div class="col-md-4">
		                                    <label for="exampleInputPassword1">促销结束时间</label>
		                                    <input onClick="WdatePicker()" type="text" name="promote_end_time" class="form-control" id="exampleInputPassword1" placeholder="促销结束时间">
	                                    </div>	                                    
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">排序数字</label>
                                        <input type="text" name="sort_num" class="form-control" id="exampleInputPassword1" value="100"placeholder="排序数字 默认100">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">系列类型</label>
                                        <input type="text" name="series" class="form-control" id="exampleInputPassword1" placeholder="请填写商品系类类型">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">商品特点</label>
                                        <input type="text" name="goods_point" class="form-control" id="exampleInputPassword1" placeholder="请填写商品特点">
                                    </div>                        
                                </div>
                                <div class="form-group">
                                    <div class="">
                                        <label for="exampleInputPassword1">商品提示</label>
                                        <input type="text" name="goods_attention" class="form-control" id="exampleInputPassword1" placeholder="请填写商品提示">
                                    </div>                        
                                </div>                                  
                                <div class="form-group">
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="logo" id="exampleInputFile">
                                    <p class="help-block">商品logo</p>
                                </div>                                 
                                <div class="form-group">
                                	<label for="exampleInputFile">是否新品</label>
									<div class="radio">
                                        <label>
                                            <input type="radio" name="is_new" id="optionsRadios1" value="1" checked="">是
                                        </label>
                                        <label>
                                            <input type="radio" name="is_new" id="optionsRadios1" value="0">否
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group">
                                	<label for="exampleInputFile">是否热卖</label>
									<div class="radio">
                                        <label>
                                            <input type="radio" name="is_hot" id="optionsRadios1" value="1">是
                                        </label>
                                        <label>
                                            <input type="radio" name="is_hot" id="optionsRadios1" value="0" checked="">否
                                        </label>
                                    </div>
                                </div>                               
                            </div><!-- /.box-body -->
                            <!-- 商品描述 -->
                            <div class="box-body" style="display:none">
	                          <!-- 加载编辑器的容器 -->
	                         	<script id="container" name="goods_desc" type="text/plain" style="width:100%"></script>
							    <!-- 配置文件 -->
							    <script type="text/javascript" src="{{ asset('/ue/ueditor.config.js') }}"></script>
							    <!-- 编辑器源码文件 -->
							    <script type="text/javascript" src="{{ asset('/ue/ueditor.all.js') }}"></script>
							    <!-- 实例化编辑器 -->
							    <script type="text/javascript">
							        var ue = UE.getEditor('container');
							    </script>                             	
                             </div>
                             <!-- 商品类型 -->
                             <div class="box-body" style="display:none">
	                          	<div class="form-group">
	                                <label for="exampleInputEmail1">商品类型</label>
	                                <select class="form-control" name="type_id">
                                        <option value="">请选择</option>
                                           @foreach($type as $v)
    	                                	<option value="{{ $v -> id }}">{{ $v -> type_name }}</option>
                                           @endforeach
	                                </select> 
	                             </div>
                                <div class="form-group">
                                <ul class="ul" style="list-style:none"></ul>
                                </div>
							 </div>
							 <div class="box-body" style="display:none">
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="one_pic" id="exampleInputFile">
                                    <p class="help-block">详情图1</p>
                                </div>
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="two_pic" id="exampleInputFile">
                                    <p class="help-block">详情图2</p>
                                </div>
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="thr_pic" id="exampleInputFile">
                                    <p class="help-block">详情图3</p>
                                </div>
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="fou_pic" id="exampleInputFile">
                                    <p class="help-block">详情图4</p>
                                </div>
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="fiv_pic" id="exampleInputFile">
                                    <p class="help-block">详情图5</p>
                                </div>
                                <div class="form-group">
                                    <img src="" alt="商品图片"><br>
                                    <label for="exampleInputFile">商品图片</label>
                                    <input type="file" name="six_pic" id="exampleInputFile">
                                    <p class="help-block">详情图6</p>
                                </div>                              
							 </div>
							<div class="box-footer">
                                <button type="submit" class="btn btn-primary">提交</button>
                                 <button type="reset" class="btn btn-primary">重置</button>
                            </div>
                        </form>
                    </div><!-- /.box -->
                </div><!--/.col (left) -->
                <!-- right column -->
            </div>   <!-- /.row -->
        </section><!-- /.content -->
    </div>
 <!-- 切换的代码 -->
 <script type="text/javascript">
 	window.onload=function()
 	{
        setInterval(function(){
            $('#jg').slideUp();
        },3000);
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
 		$('h6').on('click',function(){
 			//获取点击的第i个按钮
 			var i =$(this).index();
 			//隐藏所有的
 			$('.box-body').hide();
 			//显示第i个box-body
 			$('.box-body').eq(i).show();	
 			$(this).addClass('btn-primary');
 			$(this).siblings().removeClass('btn-primary');
 		});

        $('select[name=type_id]').on('change',function(){
            //获取所选下拉框的id
            var type_id = $(this).val();
            //根据id使用ajax 返回该类型下的属性

            if(type_id != null)
            {
                $.ajax({
                    type:"POST",
                    url:"{{ url('admin/goods/ajaxAttribute') }}",
                    data:{type_id:type_id},
                    dataType:'json',
                    success:function(data)
                    {
                        //返回的为json数据 为一个数组 里面为对象
                        var li = "";
                        //jq中遍历数组
                        $(data).each(function(k,v){
                            li += "<li>";
                            //如果这个类型是可选的就有个[+]
                            if(v.attr_type == 1)
                            {   
                                //设置点击事件 this为a节点
                                li += "<a onclick='addAttr(this);' href='#'></a>";
                            }
                            //属性名称
                            li += v.attr_name + ':';
                            //如果属性有可选值做下拉框 否则做文本框
                            if(v.attr_option_values == "")
                            {
                                li += '<input name = "attr_value['+ v.id +'][]" type="text">';
                            }else
                            {
                                //name起名 将属性的id与属性值都传过去
                                li += '<select name = "attr_value['+ v.id +'][]" class="form-control"><option value="">请选择</option>';
                                //js将字符串转换成数组
                                var attr = v.attr_option_values.split(',');
                                for(var i=0; i < attr.length; i++)
                                {
                                    li += '<option value="'+attr[i] + '">';
                                    li +=  attr[i];
                                    li += '</option>'
                                }
                                li += '</select>';
                            }
                            li += '</li>';
                           
                        });
                     $('.ul').html(li);   
                    },
                    error:function()
                    {
                        alert('没有属性');
                    }
                });
            }else
            {
                $('.ul').html();
            }   
        });

 	}
function addAttr(b)
{
    //获取li  $(节点对象)  将js节点对象转换成jq对象
    var li = $(b).parent();
    if($(b).text() == '[+]')
    {
        var newLi = li.clone(true);
        //+变-
        newLi.find("a").text('[-]');
        $(li).after(newLi);
    }else
    {
        li.remove();
    }

}

function addKuozhan()
{  
    var newA = $('#goods_name').before($('#kuozhan').clone(true));
}

 </script>
@endsection